package com.douwong.student.utils;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.douwong.student.utils.ImagFilterUtil.Base642Bitmap;

/**
 * @autor yichao
 * @time 2017/5/1111:00
 * @des 过滤显示Html数据
 */

public class HtmlUtils {

    private static final String TAG = "HtmlUtils";
    private static String brRegex="<br/>";
    private static String p1Regex="</p>";
    private static String p2Regex="<p>";
    private static String adjunctRegex = "<IMG(\\s)align=\"absmiddle\"(\\s)src=\"((\\w){32}\\.(\\w{3}))\"(\\s)>?";//附件图片正则
   private static String underlineRegex = "<span(\\s)style=\"text-decoration:(\\s)underline;\">(.*?)</span>";//过滤文字下滑线标签
    private static  String imageRegex="data:image/png;base64,(.*)";//base64过滤数据源
    /**
     * 过滤附件图片显示数据
     * @param textView
     * @param strData
     */
   public static void showItemBody(TextView textView,String strData){

       if (strData==null ||strData.equals("")){
           textView.setText("");
           return;
       }

       strData=strData.replaceAll(p1Regex,"");
       strData=strData.replaceAll(p2Regex,"");

       Pattern compile = Pattern.compile(adjunctRegex, Pattern.CASE_INSENSITIVE);
       Matcher matcher = compile.matcher(strData);
       if (matcher.find()) {
           String imageSrc = matcher.group(3);
           Log.i(TAG, "图片附件数据" + imageSrc);
       }

       String fileterAdjunctData = (strData).replaceAll(adjunctRegex, "");
       Log.i(TAG, "过滤附件后的数据  :" + fileterAdjunctData);

       //填空题过滤下划线替换——————；
       //<span style="text-decoration: underline;"> 6  </span><br/>

       Pattern compile1 = Pattern.compile(underlineRegex);
       Matcher matcher1 = compile1.matcher(fileterAdjunctData);
       while (matcher1.find()) {
           String unlineData = matcher1.group();
           Log.i(TAG, "填空题答案数据  ：" + unlineData);
       }
       String filterUnlineData = fileterAdjunctData.replaceAll(underlineRegex, "____");
       Log.i(TAG, "过滤填空题答案后数据 ：" + filterUnlineData);

       //自定义标签显示图片
       textView.setText(Html.fromHtml(filterUnlineData,mImageGetter,null));
   }

    /**
     * 过滤附件，并返回附件src 数据
     * @param strData
     * @return
     */
   public static List<String> getAdjuntData(String strData){
       List<String>  ajuntData=new ArrayList<>();
       if (strData==null ||strData.equals("")){

           return ajuntData;
       }
       Pattern compile = Pattern.compile(adjunctRegex, Pattern.CASE_INSENSITIVE);
       Matcher matcher = compile.matcher(strData);
       if (matcher.find()) {
           String imageSrc = matcher.group(3);
           Log.i(TAG, "图片附件数据" + imageSrc);
           ajuntData.add(imageSrc);
       }

       return ajuntData;
   }

    /**
     * 判读是否显示图片
     * @param strData
     * @return
     */
   public static boolean isShowImage(String strData){
       List<String> adjuntData = getAdjuntData(strData);
       if (adjuntData.size()>0){
           return true;
       }
       return false;

   }


   static Html.ImageGetter mImageGetter=new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String s) {

            Log.i(TAG,"图片数据 ："+s);

            Pattern compile = Pattern.compile(imageRegex);
            Matcher matcher = compile.matcher(s);
            while (matcher.find()){
                String imageBase64 = matcher.group(1);
                Log.i(TAG,"图片base64 数据 ："+imageBase64);
                Bitmap bitmap = Base642Bitmap(imageBase64);

                Drawable drawable =new BitmapDrawable(bitmap);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            }
            return null;
        }
    };
}
